<template>
	<view>
		<view class="form-container">
			<view class="form-item">
				<view class="label require">
					联系人
				</view>
				<input class="sun-input" v-model="groupBuy.linkMan" type="text" placeholder="请输入你的姓名" />
			</view>
			<view class="form-item">
				<view class="label require">
					手机
				</view>
				<input class="sun-input" v-model="groupBuy.phoneNumber" type="text" placeholder="请输入你的手机" />
			</view>
			<view class="form-item">
				<view class="label require">
					成团类型
				</view>
				<view class="sun-select" @click="toSelect">
					<view class="select-text">
						{{groupBuy.groupBuyTypeId==0? "请选址成团类型" : typeName}}
					</view>
					<view class="select-icon iconfont">
						&#xe65e;
					</view>
				</view>
			</view>
			<view class="form-item">
				<view class="label">
					意向小区
				</view>
				<input class="sun-input" v-model="groupBuy.wantVillage" type="text" placeholder="请输入你期望的小区" />
			</view>
			<view class="form-item">
				<view class="label">
					意向楼层
				</view>
				<input class="sun-input" v-model="groupBuy.wantLayer" type="text" placeholder="请输入你期望的楼层" />
			</view>
			<view class="form-item">
				<view class="label">
					意向户型
				</view>
				<input class="sun-input" v-model="groupBuy.wantLayout" type="text" placeholder="请输入你期望的户型" />
			</view>
			<view class="btn-box">
				<button class="btn btn-primary" type="default" @click="submit">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				groupBuy: {
					groupBuyTypeId: 0,
					floorId: null,
					wantVillage: '',
					wantLayer: '',
					wantLayout: '',
					linkMan: '',
					phoneNumber: ''
				},
				typeName: ''
			};
		},
		onLoad(event) {
			if (event.id) {
				this.groupBuy.floorId = event.id
			}
			this.getBuyTypes();
			this.groupBuy.phoneNumber = this.user.phoneNumber;
			this.groupBuy.linkMan = this.user.name;
		},
		methods: {
			getBuyTypes() {
				this.$http.get('/api/services/app/GroupBuyType/GetAllList')
					.then(res => {
						this.buyTypes = res.data.result;
					})
			},
			toSelect(){
				let self = this;
				uni.navigateTo({
					url: '../select-buytype/select-buytype',
					events:{
						selectOver:function (res) {
							console.log(res.data);
							self.typeName = res.data.name;
							self.groupBuy.groupBuyTypeId = res.data.id;
						}
					}
				})
			},
			submit(){
				if (!this.groupBuy.linkMan) {
					uni.showToast({
						title: '请输入联系人'
					});
					return;
				}
				if (!this.groupBuy.phoneNumber) {
					uni.showToast({
						title: '请输入联系电话'
					});
					return;
				}
				if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.groupBuy.phoneNumber)) {
					uni.showToast({
						title: '手机号码格式不正确'
					});
					return;
				}
				if (!this.groupBuy.groupBuyTypeId) {
					uni.showToast({
						title: '请选择成团类型'
					});
					return;
				}
				this.$http.post('/api/services/app/GroupBuy/Create',this.groupBuy)
				.then(res=>{
					uni.showToast({
						title: '恭喜你，你已成团'
					});
					const channel = this.getOpenerEventChannel();
					channel.emit("addGroupSuccess");
					uni.navigateBack({
						delta:1
					})
				});
			}
		},
		computed: {
			user() {
				return this.$store.state.session.user;
			}
		}
	}
</script>

<style lang="scss">
	.form-container {
		padding: 24rpx;
		margin: 16rpx auto;
		.form-item {
			font-size: 14px;
			.label {
				margin: 5px auto;
				&.require::after {
					content: '*';
					color: #FF0000;
				}
			}
			.sun-input {
				font-size: 14px;
				border: 1px solid $uni-border-color;
				padding: 6px;
			}
			.sun-select {
				font-size: 14px;
				border: 1px solid $uni-border-color;
				padding: 6px;
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				.select-text {
					flex-grow: 1;
					color: #808080;
				}
				.iconfont {
					font-size: 14px;
				}
			}
		}
	}
</style>
